//
//  Mine_HeaderView.m
//  SSKJ
//
//  Created by James on 2018/12/12.
//  Copyright © 2018年 刘小雨. All rights reserved.
//

#import "Mine_HeaderView.h"

@interface  Mine_HeaderView()
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *userNameButton;
@property (nonatomic, strong) UIButton *logoutButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@end

@implementation Mine_HeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bgImageView];
        [self addSubview:self.lineView];
        [self.bgImageView addSubview:self.userNameButton];
        [self.bgImageView addSubview:self.logoutButton];
        [self.bgImageView addSubview:self.titleLabel];
        [self.bgImageView addSubview:self.subTitleLabel];
        [self setupMasnory];
    }
    return self;
}

#pragma mark -- Private Method

- (void)setupMasnory {
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(ScaleH(130));
    }];
    [self.userNameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgImageView).offset(ScaleW(20));
        make.top.equalTo(self.bgImageView.mas_top).offset(ScaleH(55));
        make.right.equalTo(self.bgImageView);
        make.height.mas_equalTo(ScaleH(25));
    }];
    [self.logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userNameButton);
        make.right.equalTo(self.bgImageView.mas_right).offset(-ScaleW(20));
        make.width.height.mas_equalTo(ScaleW(25));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgImageView);
        make.top.equalTo(self).offset(ScaleH(118));
    }];
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgImageView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(ScaleH(6));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.bgImageView.mas_bottom);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(ScaleH(10));
        make.bottom.equalTo(self);
    }];
}

- (void)configureViewWithIsLogin:(BOOL)isLogin {
    [self.userNameButton setTitle:SSKJLocalized(@"请登录", nil) forState:UIControlStateNormal];
    [self.userNameButton setTitle:SSKJLocalized(@"请登录", nil) forState:UIControlStateHighlighted];
    self.titleLabel.text = @"12324123.56";
    self.subTitleLabel.text = @"账户资产(USDT)";
    if (isLogin) {
        self.titleLabel.hidden = NO;
        self.subTitleLabel.hidden = NO;
        self.logoutButton.hidden = NO;
    }else{
        self.titleLabel.hidden = YES;
        self.subTitleLabel.hidden = YES;
        self.logoutButton.hidden = YES;
    }
    [self.bgImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (isLogin) {
            make.height.mas_equalTo(ScaleH(210));
        }else{
            make.height.mas_equalTo(ScaleH(130));
        }
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.mas_bottom);
        make.bottom.equalTo(self);
    }];
    [self layoutIfNeeded];
}

#pragma mark -- Public Method

#pragma mark -- Getter Method
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]init];
        _bgImageView.image = [UIImage imageNamed:@"mine_bg_top"];
        _bgImageView.backgroundColor = [UIColor whiteColor];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = kMainColor;
    }
    return _lineView;
}

- (UIButton *)userNameButton {
    if (!_userNameButton) {
        _userNameButton = [[UIButton alloc]init];
        _userNameButton.titleLabel.font = [UIFont systemFontOfSize:ScaleFont(18)];
        [_userNameButton setTitleColor:kMainTextColor forState:UIControlStateNormal];
        [_userNameButton setTitleColor:kMainTextColor forState:UIControlStateHighlighted];
        [_userNameButton setImage:[UIImage imageNamed:@"mine_logo"] forState:UIControlStateNormal];
        [_userNameButton setImage:[UIImage imageNamed:@"mine_logo"] forState:UIControlStateHighlighted];
        _userNameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _userNameButton.titleEdgeInsets = UIEdgeInsetsMake(0, ScaleW(10), 0, 0);
        [_userNameButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_userNameButton setTitle:SSKJLocalized(@"请登录", nil) forState:UIControlStateNormal];
        [_userNameButton setTitle:SSKJLocalized(@"请登录", nil) forState:UIControlStateHighlighted];
    }
    return _userNameButton;
}

- (UIButton *)logoutButton {
    if (!_logoutButton) {
        _logoutButton = [[UIButton alloc]init];
        [_logoutButton setImage:[UIImage imageNamed:@"mine_logout"] forState:UIControlStateNormal];
        [_logoutButton setImage:[UIImage imageNamed:@"mine_logout"] forState:UIControlStateHighlighted];
        [_logoutButton addTarget:self action:@selector(logoutButton:) forControlEvents:UIControlEventTouchUpInside];
        _logoutButton.hidden = YES;
    }
    return _logoutButton;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = kMainTextColor;
        _titleLabel.font = [UIFont boldSystemFontOfSize:ScaleFont(32)];
        _titleLabel.hidden = YES;
    }
    return _titleLabel;
}

- (UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc]init];
        _subTitleLabel.textColor = kMainTextColor;
        _subTitleLabel.font = [UIFont boldSystemFontOfSize:ScaleFont(14)];
        _subTitleLabel.hidden = YES;
    }
    return _subTitleLabel;
}

#pragma mark -- private Method
- (void)clickButton:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickWithButton:)]) {
        [self.delegate clickWithButton:sender];
    }
}
- (void)logoutButton:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(logoutClickButton:)]) {
        [self.delegate logoutClickButton:sender];
    }
}
@end
